package com.example.ivideo_2211a.search

import com.example.common.mvi.BaseRepository
import com.example.common.mvi.NetworkResult
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import com.example.common.mvi.NetworkConfig
import com.example.common.mvi.Res


class SearchRepository : BaseRepository() {
    private val apiService: SearchApi by lazy {
        NetworkConfig.createApiService("http://10.161.9.80:7015")
    }

    fun getServiceVideo(
        keyWord: String,
        page: Int,
        pageSize: Int = 10
    ): Flow<NetworkResult<Res<List<SearchDataEntity>>>> {
        return safeApiCall {
            apiService.getVideoByKeyWord(keyWord, page, pageSize)
        }.map {
            when (it) {
                is NetworkResult.Success -> {
                    NetworkResult.Success(it.data)
                }
                is NetworkResult.Error -> it
                NetworkResult.Loading -> it
            }
        }
    }
}
